#ifndef __BL_IF_H
#define __BL_IF_H

#include <bl_config.h>
#include <rtdef.h>


#if defined BOOT_USING_YMODEM
#define PKG_USING_YMODEM_OTA
#endif
#if defined TBOOT_USING_HTTP
#define PKG_USING_HTTP_OTA
#endif

/* AES256 encryption algorithm option */

#define BOOT_ENCRYP_MASK        0x000F
#define BOOT_COMPRE_MASK        0x0F00

#define BOOT_ENCRYP_NONE        0x0
#define BOOT_ENCRYP_AES256      0x1

#define BOOT_COMPRE_NONE        0x0
#define BOOT_COMPRE_GZIP        (0x1 << 8)
#define BOOT_COMPRE_QUICKLZ     (0x2 << 8)
#define BOOT_COMPRE_FASTLZ      (0x3 << 8)

/* BootLoader error define */

#define BOOT_EOK                0       // NO ERR
#define BOOT_ERROR              -1      // GENERAL ERROR
#define BOOT_EMEM               -2      // NO MEMORY
#define BOOT_ECRC               -3      // CRC32 ERROR
#define BOOT_EVERIFY            -4      // VERIFY FAILED
#define BOOT_EENCRYP            -5      // ENCRYPTION ALGORITHM ERROR
#define BOOT_ECOMPRE            -6      // COMPRESSION ALGORITHM ERROR
#define BOOT_ERD                -7      // READ ERROR
#define BOOT_EWR                -8      // WRITE ERROR
#define BOOT_EER                -9      // ERASE ERROR

typedef enum {
    TB_READ_MORE,
    TB_DECRPT,
    TB_DECOMPR,
    TB_WRITE,
    TB_PRE_NEXT,
    TB_FIN,
}UpdateProgress;

typedef struct {
    rt_uint32_t magic_numb;
    rt_uint32_t tbl_ver;        // [31:24] major, [23:16] minor [15:0] reversion
    char model[8];
    rt_uint16_t flag;
	rt_uint16_t block_size;
    rt_uint32_t build_ts;
    rt_uint32_t ctx_crc;
	rt_uint32_t raw_size;
	rt_uint32_t ctx_size;
	char parti_name[12];
	char reserve1[16];
	char reserve2[12];
	rt_uint32_t header_crc;
}bl_header_info, *bl_header_info_t;

extern const struct fal_partition *dstPartition;

void initOnChipFlashPartition(void);
void initHoldKey(void);
rt_bool_t findAppPartition(void);
rt_bool_t holdBootloader(void);
rt_bool_t jump2Application(void);
rt_bool_t updateApplication(void);

#ifdef TBOOT_USING_USB
rt_bool_t updateFromUSBDisk(void);
#endif

#ifdef TBOOT_USING_YMODEM
rt_bool_t updateFromYmodem(void);
#endif

#endif // __BL_IF_H
